namespace ClassyBlog.Infrastructure.Specs.Unit
{
    using System;
    using System.Web.Mvc;

    using Machine.Specifications;
    using NSubstitute;

    using Infrastructure;
    using Web;

    [Subject(typeof(ArchivedDatesBinder))]
    public class when_trying_to_bind_archived_dates_with_null_binding_context
    {
        static ArchivedDatesBinder binder;
        static Exception exception;

        Establish context = () => binder = new ArchivedDatesBinder();

        Because of = () => exception =
            Catch.Exception(() => binder.BindModel(null, null));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    [Subject(typeof(ArchivedDatesBinder))]
    public class when_binding_archived_dates
    {
        static ModelBindingContext bindingContext;
        static ArchivedDatesBinder binder;
        static ArchivedDates archivedDates;

        Establish context = () =>
        {
            var valueProvider = Substitute.For<IValueProvider>();

            valueProvider.GetValue(ArchivedDates.YearParameterName)
                         .Returns(new ValueProviderResult(
                             2011, "2011", Culture.Current));

            valueProvider.GetValue(ArchivedDates.MonthParameterName)
                         .Returns(new ValueProviderResult(
                             4, "4", Culture.Current));

            valueProvider.GetValue(ArchivedDates.DayParameterName)
                         .Returns(new ValueProviderResult(
                             29, "29", Culture.Current));

            bindingContext = new ModelBindingContext
                                 {
                                     ValueProvider = valueProvider
                                 };

            binder = new ArchivedDatesBinder();
        };

        Because of = () =>
            archivedDates = binder.BindModel(null, bindingContext)
            as ArchivedDates;

        It should_return_archived_dates = () =>
            archivedDates.ShouldNotBeNull();
    }
}